package dk.marten.ibdrecord;


import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.util.Log;


public class DBAccess {
	
	  private SQLiteDatabase database;
	  private DBHelper dbHelper;
	  private Boolean isOpen = false;
	  
	  public DBAccess(Context context) {				  
		dbHelper = DBHelper.getInstance(context);
	  }

	  public void open() throws SQLException {
		    database = dbHelper.getWritableDatabase();
		    isOpen = true;
		  }
	  
	  public void close() {
		    dbHelper.close();
		    isOpen = false;
		  }
	  
	  public void insertOrUpdateTilstandAsync(TilstandData... tilstandDatas) {	
		  AsyncTask i = new AsyncTask() {
             @Override
             protected Object doInBackground(Object... params)
             {
            	 for(int i =0; i<params.length;i++){
            		 insertOrUpdateTilstand((TilstandData) params[i]);
            	 }
            	 close();
                return null;
             }
          };
          i.execute(tilstandDatas);
	  }

	  public TilstandData insertOrUpdateTilstand(TilstandData tilstand) {
		  	if(!isOpen) open();
		    ContentValues values = new ContentValues();
		    values.put(DBHelper.DATO, DBHelper.calenderToString(tilstand.getDato()));
		    values.put(DBHelper.WEIGHT, tilstand.getWeight());
		    values.put(DBHelper.TILSTAND, tilstand.getTilstand());
		    values.put(DBHelper.FATIGUE, tilstand.getFatigue());
		    values.put(DBHelper.SMERTE, tilstand.getSmerte());
		    values.put(DBHelper.STRESS, tilstand.getStress());
		    
		    if(tilstand.getId() == -1){
			    try{
			    	tilstand.setId(database.insert(DBHelper.TABLE_TILSTAND, null,  values));
		    	}catch(Exception e){
				    Log.d("MOE","INSERT error: "+e.getMessage());
			    }
		    }else{
			    try{
			    	database.update(DBHelper.TABLE_TILSTAND, values, "id="+tilstand.getId(), null);
			    }catch(Exception e){
				    Log.d("MOE","UPDATE error: "+e.getMessage());			    	
			    }
		    }
		   
		    return tilstand;
		  }
	  
	  public TilstandData getTilstand(long id) {
		  	if(!isOpen) open();
		   Cursor cursor = database.query(DBHelper.TABLE_TILSTAND,DBHelper.TABLE_TILSTAND_COLUMNS, DBHelper.ID + " = " + id, null,null, null, null);
		   cursor.moveToFirst();
		   TilstandData t = cursorToTilstand(cursor);
		   cursor.close();
		   return t;
		  }
	  
	  public void deleteTilstand(TilstandData tilstand) {
		  	if(!isOpen) open();
		    database.delete(DBHelper.TABLE_TILSTAND,  DBHelper.ID + " = " + tilstand.getId(), null);
		  }

	  public List<TilstandData> getAllTilstandData() {
		if(!isOpen) open();
	    List<TilstandData> tilstande = new ArrayList<TilstandData>();
		 
	    Cursor cursor = database.query(DBHelper.TABLE_TILSTAND, DBHelper.TABLE_TILSTAND_COLUMNS, null, null, null, null, DBHelper.DATO);

	    cursor.moveToFirst();
	    while (!cursor.isAfterLast()) {
	    	TilstandData tilstand = cursorToTilstand(cursor);
	    	tilstande.add(tilstand);
	      cursor.moveToNext();
	    }
	    
	    cursor.close();
	    return tilstande;
	  }

	  
	  private TilstandData cursorToTilstand(Cursor cursor) {
		  TilstandData tilstand = new TilstandData();
		  tilstand.setId(cursor.getLong(cursor.getColumnIndex(DBHelper.ID)));
		  tilstand.setDato(DBHelper.stringToCalendar(cursor.getString(cursor.getColumnIndex(DBHelper.DATO))));
		  tilstand.setWeight(cursor.getDouble(cursor.getColumnIndex(DBHelper.WEIGHT)));
		  tilstand.setTilstand(cursor.getInt(cursor.getColumnIndex(DBHelper.TILSTAND)));
		  tilstand.setFatigue(cursor.getInt(cursor.getColumnIndex(DBHelper.FATIGUE)));
		  tilstand.setSmerte(cursor.getInt(cursor.getColumnIndex(DBHelper.SMERTE)));
		  tilstand.setStress(cursor.getInt(cursor.getColumnIndex(DBHelper.STRESS)));		  
		  return tilstand;
	  }
	  
	  
	  public ToiletData insertOrUpdateToilet(ToiletData toilet) {
			if(!isOpen) open();
		    ContentValues values = new ContentValues();
		    values.put(DBHelper.DATO, DBHelper.calenderToString(toilet.getDato()));
		    values.put(DBHelper.KONSISTENS, toilet.getKonsistens());
		    values.put(DBHelper.SLIM, toilet.getSlim());
		    values.put(DBHelper.BLOD, toilet.getBlod());
		    values.put(DBHelper.TRANG, toilet.getTrang());
		    
		    if(toilet.getId() == -1){
			    try{
			    	toilet.setId(database.insert(DBHelper.TABLE_TOILET, null,  values));
		    	}catch(Exception e){
				    Log.d("MOE","INSERT error: "+e.getMessage());
			    }
		    }else{
			    try{
			    	database.update(DBHelper.TABLE_TOILET, values, "id="+toilet.getId(), null);
			    }catch(Exception e){
				    Log.d("MOE","UPDATE error: "+e.getMessage());			    	
			    }
		    }
		   
		    return toilet;
		  }
	  
	  public ToiletData getToilet(long id) {
			if(!isOpen) open();
		   Cursor cursor = database.query(DBHelper.TABLE_TOILET,DBHelper.TABLE_TOILET_COLUMNS, DBHelper.ID + " = " + id, null,null, null, null);
		   cursor.moveToFirst();
		   ToiletData t = cursorToToilet(cursor);
		   cursor.close();
		   return t;
		  }
	  
	  public void deleteTilstand(ToiletData toilet) {
			if(!isOpen) open();
		    database.delete(DBHelper.TABLE_TOILET,  DBHelper.ID + " = " + toilet.getId(), null);
		  }

	  public List<ToiletData> getAllToiletData() {
			if(!isOpen) open();
		  Log.d("MOE","getAllToiletData");
	    List<ToiletData> toilets = new ArrayList<ToiletData>();

		  Log.d("MOE","get data");
		 try{
		    Cursor cursor = database.query(DBHelper.TABLE_TOILET, DBHelper.TABLE_TOILET_COLUMNS, null, null, null, null, DBHelper.DATO);

			  Log.d("MOE","cursor done");
		    cursor.moveToFirst();
	
			  
		    while (!cursor.isAfterLast()) {
		    	ToiletData toilet = cursorToToilet(cursor);
	
				  Log.d("MOE",toilet.toString());
		    	toilets.add(toilet);
		      cursor.moveToNext();
		    }
		    
		    cursor.close();
		 }
		 catch(Exception e){
			 Log.d("MOE",e.getMessage());
			 
		 }

	    return toilets;
	  }

	  private ToiletData cursorToToilet(Cursor cursor) {
		  ToiletData toilet = new ToiletData();
		  toilet.setId(cursor.getLong(cursor.getColumnIndex(DBHelper.ID)));
		  toilet.setDato(DBHelper.stringToCalendar(cursor.getString(cursor.getColumnIndex(DBHelper.DATO))));
		  toilet.setKonsistens(cursor.getInt(cursor.getColumnIndex(DBHelper.KONSISTENS)));
		  toilet.setSlim(cursor.getInt(cursor.getColumnIndex(DBHelper.SLIM)));
		  toilet.setBlod(cursor.getInt(cursor.getColumnIndex(DBHelper.BLOD)));
		  toilet.setTrang(cursor.getInt(cursor.getColumnIndex(DBHelper.TRANG)));		  
		  return toilet;
	  }
	  
	  
	  

}
